﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace PatientInformationExtract.MainInterface.Models
{
    public class TranslationLearningDictionary
    {
        private Dictionary<string, string> _alreadyCheckedWords = new Dictionary<string, string>();
        private Dictionary<string, string> _unmodifiedWords = new Dictionary<string, string>();
        private string _dictionaryFilePath;
        private XmlDocument _xmlDoc;
        private XmlNode _rootNode;
        private DateTime _lastWriteTime;

        public TranslationLearningDictionary(string dictionaryFilePath)
        {
            try
            {
                this._dictionaryFilePath = dictionaryFilePath;
                ReloadTranslationLearningDictionary();
            }
            catch (Exception ex)
            {
                throw new Exception("Loading translation learning dicitionary: " + dictionaryFilePath + " failed.", ex);
            }
        }

        public bool TryGetTranslation(string word, out string translation)
        {
            FileInfo file = new FileInfo(this._dictionaryFilePath);
            if (!DateTime.Equals(this._lastWriteTime, file.LastWriteTime))
            {
                ReloadTranslationLearningDictionary();
            }
            string result;
            if (!this._alreadyCheckedWords.TryGetValue(word, out result))
            {
                if (!this._unmodifiedWords.TryGetValue(word, out result))
                {
                    translation = result;
                    return false;
                }
                else
                {
                    translation = "*" + result;
                }
            }
            else
            {
                translation = result;
            }
            return true;
        }

        public bool AddNewWord(string word, string translation, List<string> unknownSegments)
        {
            if (this._alreadyCheckedWords.Keys.Contains(word)||this._unmodifiedWords.Keys.Contains(word))
            {
                return false;
            }
            else
            {
                this._unmodifiedWords.Add(word, translation);
                XmlElement translationE = this._xmlDoc.CreateElement("Translation");
                translationE.SetAttribute("Chinese", word);
                translationE.SetAttribute("English", translation);
                translationE.SetAttribute("ManuallyModified", "False");
                translationE.SetAttribute("UnknownSegments", String.Join(",", unknownSegments));
                this._rootNode.AppendChild(translationE);
                this._xmlDoc.Save(this._dictionaryFilePath);
                FileInfo file = new FileInfo(this._dictionaryFilePath);
                this._lastWriteTime = file.LastWriteTime;
                return true;
            }
        }

        private void ReloadTranslationLearningDictionary()
        {
            this._alreadyCheckedWords.Clear();
            this._unmodifiedWords.Clear();
            FileInfo file = new FileInfo(this._dictionaryFilePath);
            this._lastWriteTime = file.LastWriteTime;
            this._xmlDoc = new XmlDocument();
            this._xmlDoc.Load(this._dictionaryFilePath);
            this._rootNode = this._xmlDoc.SelectSingleNode(".//TranslationLearningDictionary");
            XmlNodeList translationNodes = this._rootNode.SelectNodes(".//Translation");
            foreach (XmlNode node in translationNodes)
            {
                if (node.Attributes["ManuallyModified"].Value.Equals("True"))
                {
                    this._alreadyCheckedWords.Add(node.Attributes["Chinese"].Value, node.Attributes["English"].Value);
                }
                else
                {
                    this._unmodifiedWords.Add(node.Attributes["Chinese"].Value, node.Attributes["English"].Value);
                }               
            }
        }
    }
}
